const express = require('express')
const cors = require('cors')

const app = express()
const PORT = 3000

app.use(cors())
app.use(express.json())

const sseURL = '/fullgoal_ai_fund_filter/api/ma/chat/stream/chat'

// SSE mock接口
app.post(sseURL, (req, res) => {
  // 获取POST请求参数
  const { message, userId } = req.body
  console.log('收到POST参数:', req.body)

  // 设置SSE响应头
  res.writeHead(200, {
    'Content-Type': 'text/event-stream; charset=utf-8',
    'Cache-Control': 'no-cache',
    Connection: 'keep-alive',
    'Access-Control-Allow-Origin': '*',
    'Access-Control-Allow-Headers': 'Cache-Control'
  })

  // 模拟数据数组
  const mockThinkContent = []
  const thinkContentCount = 40
  const dataContentCount = 50

  for (let i = 0; i < thinkContentCount; i++) {
    mockThinkContent.push('这是思考内容')
  }

  const mockData = []
  for (let i = 0; i < dataContentCount; i++) {
    mockData.push('这是回复')
  }

  let thinkIndex = 0
  let dataIndex = 0

  let progress = 0
  const totalLength = mockThinkContent.length + mockData.length

  // 发送数据的函数
  const sendData = () => {
    if (progress < totalLength) {
      let data = null
      if (thinkIndex <= mockThinkContent.length) {
        data = {
          reasoningContent: mockThinkContent[thinkIndex],
          toolCallResult: null,
          content: null,
          toolCallParam: null,
          chatStatus: null,
          toolCalls: null
        }
        thinkIndex++
      }

      if (thinkIndex > mockThinkContent.length && dataIndex < mockData.length) {
        data = {
          reasoningContent: null,
          toolCallResult: null,
          content: mockData[dataIndex],
          toolCallParam: null,
          chatStatus: null,
          toolCalls: null
        }
        dataIndex++
      }

      res.write(`data:${JSON.stringify(data)}\n\n`)
      progress++
      // 每100ms发送一次数据
      setTimeout(sendData, 100)
    } else {
      /**
       * data:{"role":null,"content":null,"toolCalls":[{"index":null,"id":"call_3d475102bd004718a043a6","type":"function","function":{"name":"get_fund_filter","arguments":"{\"","output":null}}],"toolCallId":null,"name":null,"contents":null,"reasoningContent":null,"partial":null,"toolCallResult":null,"toolCallParam":null,"chatStatus":null,"message":null}

// 将下面注释中的 toolCalls 提取到 mockToolCalls 变量中
const mockToolCalls = [
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"jsonParam","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\":","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":" \"{\\","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\"select","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"ors\\\":","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"[{\\","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\"name","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\":\\\"","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"最大回","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"撤\\\",","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\"options","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\":{\\","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\"近","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"1年","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\":\\\"","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"10%","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"以下\\\"","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"},\\\"","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"mode\\\":","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\"\\\"","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"}]}\"","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"}","output":null}}],
  [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":null,"output":null}}]
]

data:{"role":null,"content":null,"toolCalls":null,"toolCallId":null,"name":null,"contents":null,"reasoningContent":null,"partial":null,"toolCallResult":"{\"columns\":[\"基金代码\",\"基金名称\",\"基金经理\",\"基金类型\",\"风险等级\",\"最大回撤-近1年\",\"阶段涨幅-近1周\",\"阶段涨幅-近1月\",\"阶段涨幅-近3月\",\"阶段涨幅-近6月\",\"阶段涨幅-今年以来\",\"阶段涨幅-近1年\",\"阶段涨幅-近2年\",\"阶段涨幅-近3年\",\"阶段涨幅-近5年\",\"阶段涨幅-成立以来\",\"年化收益-成立以来\"],\"data\":[[\"009108.OF\",\"富国红利精选混合(QDII)人民币\",\"王菀宜\",\"QDII混合型基金\",\"R4-中高风险\",9.91,1.02,6.14,16.15,31.07,28.96,37.41,31.97,19.28,32.52,71.28,10.74],[\"100035.OF\",\"富国优化增强债券A/B\",\"刘兴旺\",\"混合债券型基金\",\"R2-中低风险\",8.06,0.15,4.01,7.72,13.83,16.81,37.03,26.39,23.02,25.36,151.92,5.89],[\"008372.OF\",\"富国阿尔法两年持有期混合\",\"张弘\",\"偏股混合型基金\",\"R3-中风险\",8.68,3.48,10.64,18.11,25.98,22.35,34.99,13.27,18.81,40.37,78.28,10.87],[\"001186.OF\",\"富国文体健康股票A\",\"林庆\",\"普通股票型基金\",\"R3-中风险\",9.38,-0.94,1.0,9.64,8.83,10.05,24.32,13.07,31.24,33.23,163.8,9.93],[\"011164.OF\",\"富国兴远优选12个月持有期混合A\",\"林庆\",\"偏股混合型基金\",\"R3-中风险\",9.5,-1.11,1.41,9.9,8.47,10.35,22.92,13.52,22.6,\"-\",1.16,0.26],[\"002908.OF\",\"富国睿利定期开放混合型发起式A\",\"袁宜\",\"偏债混合型基金\",\"R3-中风险\",5.26,-0.87,3.73,8.14,10.32,9.34,21.1,10.9,4.31,16.23,47.5,4.49],[\"011830.OF\",\"富国天恒混合A\",\"林庆\",\"偏股混合型基金\",\"R3-中风险\",9.77,-0.49,1.92,10.52,9.14,11.26,21.02,13.92,30.46,\"-\",26.72,6.96],[\"013420.OF\",\"富国鑫汇养老目标日期2045五年持有期混合发起式(FOF)A\",\"王登元\",\"养老目标FOF\",\"R3-中风险\",9.8,1.34,4.75,9.27,10.54,10.07,20.12,7.62,4.89,\"-\",5.6,1.64],[\"161010.OF\",\"富国天丰强化债券(LOF)A\",\"张明凯\",\"混合债券型基金\",\"R2-中低风险\",6.41,-0.56,3.0,4.45,9.29,9.55,19.6,14.99,15.61,31.46,185.08,6.44],[\"005121.OF\",\"富国兴利增强债券A\",\"陈斯扬\",\"混合债券型基金\",\"R2-中低风险\",8.69,-0.67,2.76,5.36,4.62,5.51,18.43,11.25,8.47,26.98,62.7,6.38]],\"types\":{\"基金代码\":\"str\",\"基金名称\":\"str\",\"基金类型\":\"str\",\"基金经理\":\"str\",\"年化收益-成立以来\":\"rate\",\"最大回撤-近1年\":\"percent\",\"阶段涨幅-今年以来\":\"rate\",\"阶段涨幅-成立以来\":\"rate\",\"阶段涨幅-近1周\":\"rate\",\"阶段涨幅-近1年\":\"rate\",\"阶段涨幅-近1月\":\"rate\",\"阶段涨幅-近2年\":\"rate\",\"阶段涨幅-近3年\":\"rate\",\"阶段涨幅-近3月\":\"rate\",\"阶段涨幅-近5年\":\"rate\",\"阶段涨幅-近6月\":\"rate\",\"风险等级\":\"str\"}}","toolCallParam":"{\"jsonParam\": \"{\\\"selectors\\\":[{\\\"name\\\":\\\"最大回撤\\\",\\\"options\\\":{\\\"近1年\\\":\\\"10%以下\\\"},\\\"mode\\\":\\\"\\\"}]}\"}","chatStatus":"stop","message":null}


       * 
       */

      const mockToolCallData = [
        [{"index":null,"id":"call_3d475102bd004718a043a6","type":"function","function":{"name":"get_fund_filter","arguments":"{\"","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"jsonParam","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\":","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":" \"{\\","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\"select","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"ors\\\":","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"[{\\","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\"name","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\":\\\"","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"最大回","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"撤\\\",","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\"options","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\":{\\","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\"近","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"1年","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\":\\\"","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"10%","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"以下\\\"","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"},\\\"","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"mode\\\":","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"\\\"\\\"","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"}]}\"","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":"}","output":null}}],
        [{"index":null,"id":"","type":"function","function":{"name":null,"arguments":null,"output":null}}]
      ]

      const mockToolCallDataLength = mockToolCallData.length
      let mockToolCallDataIndex = 0

      const jsonParam = JSON.stringify({
        jsonParam: JSON.stringify({
          selectors: [
            {
              name: "最大回撤",
              options: {
                "近1年": "10%以下"
              },
              mode: ""
            }
          ]
        })
      });

      const jsonResult = JSON.stringify({
        columns: [
          "基金代码", "基金名称", "基金经理", "基金类型", "风险等级", "最大回撤-近1年",
          "阶段涨幅-近1周", "阶段涨幅-近1月", "阶段涨幅-近3月", "阶段涨幅-近6月",
          "阶段涨幅-今年以来", "阶段涨幅-近1年", "阶段涨幅-近2年", "阶段涨幅-近3年",
          "阶段涨幅-近5年", "阶段涨幅-成立以来", "年化收益-成立以来"
        ],
        data: [
          ["009108.OF","富国红利精选混合(QDII)人民币","王菀宜","QDII混合型基金","R4-中高风险",9.91,1.02,6.14,16.15,31.07,28.96,37.41,31.97,19.28,32.52,71.28,10.74],
          ["100035.OF","富国优化增强债券A/B","刘兴旺","混合债券型基金","R2-中低风险",8.06,0.15,4.01,7.72,13.83,16.81,37.03,26.39,23.02,25.36,151.92,5.89],
          ["008372.OF","富国阿尔法两年持有期混合","张弘","偏股混合型基金","R3-中风险",8.68,3.48,10.64,18.11,25.98,22.35,34.99,13.27,18.81,40.37,78.28,10.87],
          ["001186.OF","富国文体健康股票A","林庆","普通股票型基金","R3-中风险",9.38,-0.94,1.0,9.64,8.83,10.05,24.32,13.07,31.24,33.23,163.8,9.93],
          ["011164.OF","富国兴远优选12个月持有期混合A","林庆","偏股混合型基金","R3-中风险",9.5,-1.11,1.41,9.9,8.47,10.35,22.92,13.52,22.6,"-",1.16,0.26],
          ["002908.OF","富国睿利定期开放混合型发起式A","袁宜","偏债混合型基金","R3-中风险",5.26,-0.87,3.73,8.14,10.32,9.34,21.1,10.9,4.31,16.23,47.5,4.49],
          ["011830.OF","富国天恒混合A","林庆","偏股混合型基金","R3-中风险",9.77,-0.49,1.92,10.52,9.14,11.26,21.02,13.92,30.46,"-",26.72,6.96],
          ["013420.OF","富国鑫汇养老目标日期2045五年持有期混合发起式(FOF)A","王登元","养老目标FOF","R3-中风险",9.8,1.34,4.75,9.27,10.54,10.07,20.12,7.62,4.89,"-",5.6,1.64],
          ["161010.OF","富国天丰强化债券(LOF)A","张明凯","混合债券型基金","R2-中低风险",6.41,-0.56,3.0,4.45,9.29,9.55,19.6,14.99,15.61,31.46,185.08,6.44],
          ["005121.OF","富国兴利增强债券A","陈斯扬","混合债券型基金","R2-中低风险",8.69,-0.67,2.76,5.36,4.62,5.51,18.43,11.25,8.47,26.98,62.7,6.38]
        ],
        types: {
          "基金代码": "str",
          "基金名称": "str",
          "基金类型": "str",
          "基金经理": "str",
          "年化收益-成立以来": "rate",
          "最大回撤-近1年": "percent",
          "阶段涨幅-今年以来": "rate",
          "阶段涨幅-成立以来": "rate",
          "阶段涨幅-近1周": "rate",
          "阶段涨幅-近1年": "rate",
          "阶段涨幅-近1月": "rate",
          "阶段涨幅-近2年": "rate",
          "阶段涨幅-近3年": "rate",
          "阶段涨幅-近3月": "rate",
          "阶段涨幅-近5年": "rate",
          "阶段涨幅-近6月": "rate",
          "风险等级": "str"
        }
      })

      // 发送结束信号
      const endData = {
        reasoningContent: null,
        content: null,
        toolCallParam: JSON.stringify({ jsonParam }),
        toolCallResult: jsonResult,
        chatStatus: 'stop',
        toolCalls: null
      }

      res.write(
        `data:${JSON.stringify({
          reasoningContent: null,
          content: null,
          toolCallParam: null,
          toolCallResult: null,
          chatStatus: null,
          toolCalls: null
        })}\n\n`
      )

      while (mockToolCallDataIndex < mockToolCallDataLength) {
        const toolCallData = mockToolCallData[mockToolCallDataIndex]
        res.write(`data:${JSON.stringify({
          reasoningContent: null,
          content: null,
          toolCallParam: null,
          toolCallResult: null,
          chatStatus: null,
          toolCalls: toolCallData
        })}\n\n`)
        mockToolCallDataIndex++
      }

      setTimeout(() => {
        res.write(`data:${JSON.stringify(endData)}\n\n`)
        res.end()
      }, 3000)
    }
  }

  setTimeout(() => {
    // 开始发送数据
    sendData()
  }, 2000)

  // 处理客户端断开连接
  req.on('close', () => {
    console.log('客户端断开连接')
  })
})

app.listen(PORT, () => {
  console.log(`SSE Mock服务器运行在 http://localhost:${PORT}`)
  console.log(`SSE接口地址: http://localhost:${PORT}${sseURL}`)
})
